package com.zheng.utils.xmlUtil;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

public class XmlToMap { 
	public static Map<String, Object> xmlToMap(String xml) throws DocumentException{ 
		Document doc = DocumentHelper.parseText(xml);
		System.out.println(doc.asXML());
		Map<String, Object> map = new HashMap<String, Object>();
		if(doc == null) return map;
		Element root = doc.getRootElement();
		System.out.println();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();
			List list = e.elements();
			if(list.size() > 0){
				map.put(e.getName(), xmlToMap(e));
			}else
				map.put(e.getName(), e.getText());
		}
		Map<String, Object> mapRoot = new HashMap<String, Object>();
		mapRoot.put(root.getName(), map);
        return mapRoot;
	}
     @SuppressWarnings("unchecked")
     public static Map xmlToMap(Element e){
    	 Map map = new HashMap();
    	 List list = e.elements();
    	 if(list.size() > 0){
    		 for (int i = 0;i < list.size(); i++) {
    			 Element iter = (Element) list.get(i);
    			 List mapList = new ArrayList();
                 
    			 if(iter.elements().size() > 0){
    				 Map m = xmlToMap(iter);
    				 if(map.get(iter.getName()) != null){
    					 Object obj = map.get(iter.getName());
    					 if(!obj.getClass().getName().equals("java.util.ArrayList")){
    						 mapList = new ArrayList();
    						 mapList.add(obj);
    						 mapList.add(m);
    					 }
    					 if(obj.getClass().getName().equals("java.util.ArrayList")){
    						 mapList = (List) obj;
    						 mapList.add(m);
    					 }
    					 map.put(iter.getName(), mapList);
    				 }else
    					 map.put(iter.getName(), m);
    			 }
    			 else{
    				 if(map.get(iter.getName()) != null){
    					 Object obj = map.get(iter.getName());
    					 if(!obj.getClass().getName().equals("java.util.ArrayList")){
    						 mapList = new ArrayList();
    						 mapList.add(obj);
    						 mapList.add(iter.getText());
    					 }
    					 if(obj.getClass().getName().equals("java.util.ArrayList")){
    						 mapList = (List) obj;
    						 mapList.add(iter.getText());
    					 }
    					 map.put(iter.getName(), mapList);
    				 }else
    					 map.put(iter.getName(), iter.getText());
    			 }
    		 }
    	 }else
    		 map.put(e.getName(), e.getText());
    	 return map;
     }
     
     public static void main(String[] args) throws DocumentException{
    	 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><head>serviceId001</head><body><rtnCode>02</rtnCode><rtnMsg>查询失败</rtnMsg><idWltCloudDistrict>专区id</idWltCloudDistrict><districtName>专区名称</districtName><validateDateStart>专区有效期开始</validateDateStart><validateDateEnd>专区有效期结束</validateDateEnd><status>专区状态(00-待发布;01-销售中; 02-已下架;)</status><partnerId>合作伙伴id</partnerId><channelId>渠道id</channelId><areaId>商圈专区id</areaId><prodKindList><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx><prodKindList><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx></prodKind><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx></prodKind></prodKindList></prodKind><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx><prodKindList><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx></prodKind><prodKind><idWltCloudProdKind>类目ID</idWltCloudProdKind><prodKindName>类目名称</prodKindName><prodKindIdx>类目序号</prodKindIdx></prodKind></prodKindList></prodKind></prodKindList></body></root>";
    	 Map<String, Object> map = xmlToMap(xml);
    	 
    	 System.out.println(map.toString());
    	 
    	 System.out.println(SearchMapByKey.get(map, "areaId"));;
     }
} 
